package com.fh.db.oracle;

import com.fh.db.*;
import com.fh.db.base.BaseDbPackage;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author qiujunda
 * @Date 22/07/2020
 * @Description
 */
public class OracleDbPackage extends BaseDbPackage {
    private DbSource dbSource;

    @Override
    public DbSource getDbSource() {
        return dbSource;
    }

    public void setDbSource(DbSource dbSource) {
        this.dbSource = dbSource;
    }

    @Override
    public List<DbPackageDeclare> getDbPackageDeclare() {
        return (List<DbPackageDeclare>) SqlRunner.preparedSqlResult(dbSource,dbSource.getSqlSet().packageDeclearPreSql(),new String[]{this.getPackageName(),this.getOwner()},
                resultSet -> {
                    List<DbPackageDeclare> result=new ArrayList<>();
                    try{
                        while(resultSet.next()){
                            OracleDbPackageDeclare oracleDbPackageDeclare=new OracleDbPackageDeclare();
                            oracleDbPackageDeclare.setContent(resultSet.getString("TEXT"));
                            oracleDbPackageDeclare.setLineId(resultSet.getBigDecimal("LINE").intValue());
                            result.add(oracleDbPackageDeclare);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    return result;
                });
    }

    @Override
    public List<DbPackageBody> getDbPackageBody() {
        return (List<DbPackageBody>) SqlRunner.preparedSqlResult(dbSource,dbSource.getSqlSet().packageBodyPreSql(),new String[]{this.getPackageName(),this.getOwner()},
                resultSet -> {
                    List<DbPackageBody> result=new ArrayList<>();
                    try{
                        while(resultSet.next()){
                            OracleDbPackageBody oracleDbPackageBody=new OracleDbPackageBody();
                            oracleDbPackageBody.setContent(resultSet.getString("TEXT"));
                            oracleDbPackageBody.setLineId(resultSet.getBigDecimal("LINE").intValue());
                            result.add(oracleDbPackageBody);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    return result;
                });
    }
}
